package liecai_class.day06;

import java.util.concurrent.CountDownLatch;

/**
 * 闭锁 CountDownLatch
 */
public class Demo04 {
    private static CountDownLatch cdl = new CountDownLatch(3);
    public static void main(String[] args) throws InterruptedException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    System.out.println("弟弟买米回来了..");
                    cdl.countDown();//将cdl中的计数值-1
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    System.out.println("姐姐买菜回来了..");
                    cdl.countDown();//将cdl中的计数值-1
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                    System.out.println("爸爸买锅回来了..");
                    cdl.countDown();//将cdl中的计数值-1
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        //阻塞，直到cdl中的计数值归0,归零时阻塞放开，继续执行
        cdl.await();
        System.out.println("妈妈开始做饭..");
    }
}
